unit uBaseDataForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ubaseform, ComCtrls, Menus, ImgList, StdActns, ActnList, DB,
  DBCtrls, Buttons, ExtCtrls, ToolWin, StdCtrls, Mask, Grids, DBGrids,
  DBClient;

type
  TfBaseDataForm = class(TfBaseForm)
    StatusBar: TStatusBar;
    CoolBar: TCoolBar;
    paToolsBrowse: TPanel;
    btIncluir: TSpeedButton;
    btEditar: TSpeedButton;
    btAtualizar: TSpeedButton;
    btExcluir: TSpeedButton;
    btToolBrowse: TSpeedButton;
    SpeedButton3: TSpeedButton;
    DBNavigator: TDBNavigator;
    paToolsEdit: TPanel;
    btCancelar: TSpeedButton;
    btGravar: TSpeedButton;
    SpeedButton1: TSpeedButton;
    bToolDataEdit: TSpeedButton;
    btAjuda: TSpeedButton;
    ActionList: TActionList;
    DataSetFirst: TAction;
    DataSetPrior: TAction;
    DataSetNext: TAction;
    DataSetLast: TAction;
    DataSetInsert: TAction;
    DataSetDelete: TAction;
    DataSetEdit: TAction;
    DataSetPost: TAction;
    DataSetCancel: TAction;
    DataSetRefresh: TAction;
    actClearField: TAction;
    actLocate: TAction;
    actHelp: TAction;
    actListRefresh: TAction;
    actForm: TAction;
    actLocateDataset: TAction;
    actCountRecord: TAction;
    actFilter: TAction;
    actOrder: TAction;
    actOrderDesc: TAction;
    actMultiSelect: TAction;
    actMultiDelete: TAction;
    actCopySelect: TAction;
    actExportRTF: TAction;
    actExportPDF: TAction;
    actExportExcel: TAction;
    actExportXML: TAction;
    actExportHTML: TAction;
    actExportTXT: TAction;
    actPrintConfig: TFilePrintSetup;
    actPrint: TAction;
    actPrintTextMode: TAction;
    DataSetPostContinue: TAction;
    ImageListAct: TImageList;
    PopupMenu: TPopupMenu;
    mniClearField: TMenuItem;
    mniSep1: TMenuItem;
    mniLocate: TMenuItem;
    mniFilter: TMenuItem;
    mniOrder: TMenuItem;
    mniOrderDec: TMenuItem;
    mniSep2: TMenuItem;
    mniListRefresh: TMenuItem;
    mniForm: TMenuItem;
    mniLocateDataset: TMenuItem;
    mniSep3: TMenuItem;
    mniCountRecord: TMenuItem;
    mniMultiSelect: TMenuItem;
    mniMultiDelete: TMenuItem;
    mniCopySelect: TMenuItem;
    Exportarseleo1: TMenuItem;
    mniExportRTF: TMenuItem;
    mniExportPDF: TMenuItem;
    mniExportExcel: TMenuItem;
    mniExportXML: TMenuItem;
    actExportHTML1: TMenuItem;
    XT1: TMenuItem;
    mniSep4: TMenuItem;
    mniPrintConfig: TMenuItem;
    mniPrint: TMenuItem;
    mniPrintTextMode: TMenuItem;
    PageControlDataForm: TPageControl;
    tshPesquisar: TTabSheet;
    tshMaintein: TTabSheet;
    PanelMaintein: TPanel;
    PageControlMaintein: TPageControl;
    CoolBar1: TCoolBar;
    eSearchConsole: TMaskEdit;
    PanelGridSearch: TPanel;
    bSearchExecute: TBitBtn;
    bSearchClear: TBitBtn;
    bToolSearch: TSpeedButton;
    PopupMenuSearch: TPopupMenu;
    MenuItem12: TMenuItem;
    MenuItem13: TMenuItem;
    MenuItem14: TMenuItem;
    MenuItem15: TMenuItem;
    MenuItem16: TMenuItem;
    MenuItem17: TMenuItem;
    MenuItem18: TMenuItem;
    MenuItem19: TMenuItem;
    MenuItem20: TMenuItem;
    MenuItem21: TMenuItem;
    MenuItem22: TMenuItem;
    MenuItem23: TMenuItem;
    MenuItem24: TMenuItem;
    MenuItem25: TMenuItem;
    MenuItem26: TMenuItem;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure DataSetInsertExecute(Sender: TObject);
    procedure DataSetDeleteExecute(Sender: TObject);
    procedure DataSetEditExecute(Sender: TObject);
    procedure DataSetPostExecute(Sender: TObject);
    procedure DataSetCancelExecute(Sender: TObject);
    procedure DataSetRefreshExecute(Sender: TObject);
    procedure DataSetPostContinueExecute(Sender: TObject);
    procedure tshMainteinShow(Sender: TObject);
    procedure DataSourceStateChange(Sender: TObject);
    procedure btToolBrowseClick(Sender: TObject);
    procedure bToolDataEditClick(Sender: TObject);
    procedure bToolSearchClick(Sender: TObject);
    procedure bSearchClearClick(Sender: TObject);
    procedure bSearchExecuteClick(Sender: TObject);
  private
    { Private declarations }
    FDBGridSearch: TDBGrid;
    FDataSourceSearch: TDataSource;
    FDataSourceMaintein: TDataSource;
    procedure ButtonMenuPullDown(Button: TSpeedButton; ButtonMenu: TPopupMenu);
    function getWhereCondition(aValue: string): string;
  public
    { Public declarations }
  end;

var
  fBaseDataForm: TfBaseDataForm;

implementation

uses uFunction;

{$R *.dfm}

procedure TfBaseDataForm.FormCreate(Sender: TObject);
begin

  PageControlDataForm.ActivePageIndex := 0;

  FDBGridSearch := TDBGrid.Create(PanelGridSearch);
  FDBGridSearch.Parent     := PanelGridSearch;
  FDBGridSearch.DataSource := FDataSourceSearch;
  FDBGridSearch.Align      := alClient;
  FDBGridSearch.Options    := FDBGridSearch.Options - [dgEditing];
  FDBGridSearch.ReadOnly   := True;

  FDataSourceSearch   := TDataSource.Create(Self);

  FDataSourceMaintein := TDataSource.Create(Self);
  FDataSourceMaintein.OnStateChange := DataSourceStateChange;

  inherited;

//  ClientDataSet1.CreateDataSet;
//  FDataSourceSearch.DataSet := ClientDataSet1;
  FDBGridSearch.DataSource := FDataSourceSearch;
end;

procedure TfBaseDataForm.FormDestroy(Sender: TObject);
begin
  FreeAndNil( FDBGridSearch );
  FreeAndNil( FDataSourceSearch );
  inherited;
end;

procedure TfBaseDataForm.FormActivate(Sender: TObject);
begin
  inherited;

  if eSearchConsole.CanFocus then
    eSearchConsole.SetFocus;
end;

procedure TfBaseDataForm.DataSetInsertExecute(Sender: TObject);
begin
  inherited;
  FDataSourceMaintein.DataSet.Insert;
end;

procedure TfBaseDataForm.DataSetDeleteExecute(Sender: TObject);
begin
  inherited;
  FDataSourceMaintein.DataSet.Delete;
end;

procedure TfBaseDataForm.DataSetEditExecute(Sender: TObject);
begin
  inherited;
  FDataSourceMaintein.DataSet.Edit;
end;

procedure TfBaseDataForm.DataSetPostExecute(Sender: TObject);
begin
  inherited;
  FDataSourceMaintein.DataSet.Post;
end;

procedure TfBaseDataForm.DataSetCancelExecute(Sender: TObject);
begin
  inherited;
  FDataSourceMaintein.DataSet.Cancel;
end;

procedure TfBaseDataForm.DataSetRefreshExecute(Sender: TObject);
begin
  inherited;
  FDataSourceMaintein.DataSet.Refresh;
end;

procedure TfBaseDataForm.DataSetPostContinueExecute(Sender: TObject);
begin
  inherited;
  FDataSourceMaintein.DataSet.Insert;
end;

procedure TfBaseDataForm.tshMainteinShow(Sender: TObject);
begin
  inherited;

  CoolBar.Height := paToolsEdit.Height;

  paToolsEdit.Visible := FDataSourceMaintein.State in dsEditModes;
  paToolsBrowse.Visible := not paToolsEdit.Visible;
end;

procedure TfBaseDataForm.DataSourceStateChange(Sender: TObject);
begin
  inherited;
  DataSetPost.Enabled         := FDataSourceMaintein.State in dsEditModes;
  DataSetCancel.Enabled       := FDataSourceMaintein.State in dsEditModes;
  DataSetPostContinue.Enabled := FDataSourceMaintein.State in dsEditModes;

  DataSetFirst.Enabled  := not (FDataSourceMaintein.State in dsEditModes);
  DataSetPrior.Enabled  := not (FDataSourceMaintein.State in dsEditModes);
  DataSetNext.Enabled   := not (FDataSourceMaintein.State in dsEditModes);
  DataSetLast.Enabled   := not (FDataSourceMaintein.State in dsEditModes);
  DataSetRefresh.Enabled:= not (FDataSourceMaintein.State in dsEditModes);
  DataSetInsert.Enabled := not (FDataSourceMaintein.State in dsEditModes);
  DataSetDelete.Enabled := not (FDataSourceMaintein.State in dsEditModes);
  DataSetEdit.Enabled   := not (FDataSourceMaintein.State in dsEditModes);

  CoolBar.Height := paToolsEdit.Height;

  paToolsEdit.Visible := FDataSourceMaintein.State in dsEditModes;
  paToolsBrowse.Visible := not paToolsEdit.Visible;

end;

procedure TfBaseDataForm.ButtonMenuPullDown(Button: TSpeedButton; ButtonMenu: TPopupMenu);
var
  APoint: TPoint;
begin
  APoint := Button.ClientToScreen(Point(0, Button.ClientHeight));
  ButtonMenu.Popup(APoint.X, APoint.Y);
end;

procedure TfBaseDataForm.btToolBrowseClick(Sender: TObject);
begin
  ButtonMenuPullDown(TSpeedButton(Sender), PopupMenu);
end;

procedure TfBaseDataForm.bToolDataEditClick(Sender: TObject);
begin
  inherited;
  ButtonMenuPullDown(TSpeedButton(Sender), PopupMenu);
end;

procedure TfBaseDataForm.bToolSearchClick(Sender: TObject);
begin
  inherited;
  ButtonMenuPullDown(TSpeedButton(Sender), PopupMenuSearch);
end;

procedure TfBaseDataForm.bSearchClearClick(Sender: TObject);
begin
  inherited;
  eSearchConsole.Clear;
  FDataSourceSearch.DataSet.Close;
end;

function TfBaseDataForm.getWhereCondition(aValue: string): string;
var
  i: integer;
  return: string;
begin
  return := '';
  for i := 0 to FDBGridSearch.Columns.Count - 1 do
    return := FDBGridSearch.Columns[i].FieldName;

  result := return;
end;

procedure TfBaseDataForm.bSearchExecuteClick(Sender: TObject);
var
  sqlText: string;
begin
  inherited;
  //

  sqlText := getWhereCondition(eSearchConsole.Text);

  TFunction.ShowAlert(sqlText);

end;

end.
